package com.lune.blog.system.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lune.blog.base.domain.system.SysRole;
import com.lune.blog.base.domain.system.SysUser;
import com.lune.blog.system.mapper.RoleMapper;
import com.lune.blog.system.service.RoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author yanjizhw@163.com
 * @date 2021/11/16 21:26
 * 描述:
 * @since 1.0.0
 */
@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, SysRole> implements RoleService  {

    @Resource
    private UserRoleServiceImpl userRoleService;

    @Override
    public Set<String> getRolePermission(Long userId) {

        Set<String> roles = new HashSet<String>();
        // 管理员所有权限
        if (SysUser.isAdmin(userId)) {
            roles.add("admin");
        } else {
            List<Long> roleIds = userRoleService.getRoleIdsByUserId(userId);
            List<SysRole> roleList = baseMapper.selectList(new QueryWrapper<SysRole>().in("role_id", roleIds));
            for (SysRole role: roleList) {
                if (ObjectUtil.isNotNull(role)) {
                    roles.addAll(Arrays.asList(role.getRoleKey().trim().split(",")));
                }
            }
        }
        return roles;
    }
}
